package com.yincheng.oms.bussiness.order;

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yincheng.oms.bussiness.util.Tools;
import com.yincheng.oms.bussiness.util.dictionary.DictionaryService;

@Controller
@RequestMapping("/orderList")
public class OrderListController {

	@Autowired
	private OrderListService orderListService;
	
	@Autowired
	private DictionaryService dictionaryService;
	
	@RequestMapping(value = "/listPage.html", method = RequestMethod.GET)
	public String listPage(Model model){
		setView(model);
		return "business/order/orderlist";
	}
	
	@RequestMapping(value = "/getList.json", method = RequestMethod.GET)
	@ResponseBody
	public String list(QueryParamBean bean){
		Pair<Integer,List<QueryResultRowBean>> pair= orderListService.getListByParam(bean);
		return Tools.toEasyuiDataGrid(pair.getLeft(), pair.getRight());
	}
	
	public void setView(Model model) {
		model.addAttribute("logStatus", dictionaryService.getDicByModel("log_status"));
	}
	
	@RequestMapping(value="/download", method=RequestMethod.GET)
	public void download(QueryParamBean bean,HttpServletResponse response) throws IOException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String fileName = sdf.format(new Date())+".xls";
		
		response.setContentType("multipart/form-data");
		response.setHeader("Content-Disposition", "attachment;filename=" + fileName);

		OutputStream os = response.getOutputStream();
		orderListService.export(bean).export(os);
	}
}
